package com.ruoyi.common.utils.wxSend.weixinmoban;


import cn.hutool.json.JSONUtil;
import com.ruoyi.common.utils.RedisUtils;

import com.ruoyi.common.utils.wxSend.common.Constant;
import com.ruoyi.common.utils.wxSend.common.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.TreeMap;

@Controller
@RequestMapping("/wxtz/")
public class WeixinFsContronller {
    @Autowired
    private RedisUtils redisUtils;
    @RequestMapping("sendWarningByWechat")
    @ResponseBody
    public String sendWarningByWechat(String openId, String content, String alarmDescriptions, String sendDateTime) {
        //获取redis中存的access_token
        String access_token = String.valueOf(redisUtils.get("access_token"));
        StringBuffer resBuff = new StringBuffer();
        try {
            String templateMsgUrl = Constant.templateMsgUrl;
            System.out.println(Constant.getAccess_token_url());
           // String resultInfo = HttpClientUtil.doGet(Constant.getAccess_token_url());

            templateMsgUrl = templateMsgUrl.replace("ACCESS_TOKEN", access_token);
           // templateMsgUrl = templateMsgUrl.replace("ACCESS_TOKEN", resultInfo);
            //封装请求体
           // Template template = new Template();
            TemplateMessage template = new TemplateMessage();
            template.setTemplate_id("An3-s0hltLVyysZOSSNTkflokVZ7VYhbjSmJ7V8JRQk");
            // 设置点击跳转的路径
            template.setUrl("http://mp.weixin.qq.com");
            // 主要是这里， 设置小程序的appid和转发的页面
            TreeMap<String, String> miniprograms = new TreeMap<String, String>();
            miniprograms.put("appid","wx19b86a5fba0825ce");
            miniprograms.put("pagepath","/pages/index/index?temp=1");// 注意，这里是支持传参的！！！
            template.setMiniprogram(miniprograms);
            //List<TpParam> templateParams = new ArrayList<>();
            // 设置消息内容和对应的颜色
            TreeMap<String, TreeMap<String, String>> params = new TreeMap<String, TreeMap<String, String>>();
           // TpParam first = new TpParam("first", content + "\\r\\n", "#DB1A1B");
            //TpParam keyword1 = new TpParam("thing1", "test" + "\\r\\n", "#DB1A1B");
            //TpParam keyword2 = new TpParam("thing7",  "test1", "#DB1A1B");
            //TpParam keyword3 = new TpParam("phone_number5", "1234567891", "#DB1A1B");
            //TpParam remark = new TpParam("time4", "2023-12-25", "#DB1A1B");
            params.put("thing1", TemplateMessage.item("test", "#DB1A1B"));
            params.put("thing7", TemplateMessage.item("test1", "#DB1A1B"));
            params.put("phone_number5", TemplateMessage.item("1234567891", "#DB1A1B"));
            params.put("time4", TemplateMessage.item("2023-12-25", "#DB1A1B"));
            template.setData(params);
           // templateParams.add(first);
            //templateParams.add(keyword1);
            //templateParams.add(keyword2);
            //templateParams.add(keyword3);
            //templateParams.add(remark);
            //template.setTpParamList(templateParams);

            template.setTouser("ohPh16i5v1Qo4yDB1V2EueDL73kc");
            //JSONObject data = JSONObject.fromObject(temp);
            String paramStr = JSONUtil.toJsonStr(template);

           // String paramStr = template.toJSON();
            //System.out.println(paramStr);
            //System.out.println(templateMsgUrl);
            String resJson = HttpClientUtil.doTemplateMsgPost(templateMsgUrl, paramStr);
            System.out.println(resJson);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "发送成功";
    }
}
